/*
 This program is submitted as part of an assignment in the
 J2E
 Fall Semester 2010-2011 
 School of Computer Science
 Interdisciplinary Center, Hertzelia, Israel 

 http://www.idc.ac.il 

 Exercise No.         : 1
 File Name            : GameModel.java
 Names (First Last)   : Itay Sorani, Nadav Ziv, Yonathan Zarsky
 Student No.          : 066530254, 039937883, 039692918
 */

package com.example.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* An array which holds a reference to each model in the game.
* @param models a private member, holds a reference to each model in the game..
* @param id the next free id to be assigned.
*/
public class GameModel {
	
	private static final int ADD_MODEL_METHOD = 1;
	private static final int REMOVE_MODEL_METHOD = 2;
	private static final int GET_MODEL_METHOD = 3;
	private static final int GET_NUM_OF_MODELS_METHOD = 0;
	private List<Figure> models;
	private int numberOfModels;
	private List<Figure> synchronizedModelsList;
	
	public GameModel() {
		models = new ArrayList<Figure>();
		synchronizedModelsList = Collections.synchronizedList(models);
		numberOfModels = 0;
		
	}
	
	/*
	 * Adds a model to the model list
	 */
	public synchronized void addFigure (Figure figure) {
		accessGameModel(ADD_MODEL_METHOD, figure);
	}
	
	/*
	 * removes a model from the model list
	 */
	public synchronized void removeModel (Figure figure) {
		accessGameModel(REMOVE_MODEL_METHOD,figure);
	}
	
	public synchronized Object accessGameModel (int methodToExecute,Object arg) {
		if (methodToExecute == ADD_MODEL_METHOD) {
			synchronizedModelsList.add((Figure)arg);
			//((Figure)arg).setId(numberOfModels);
			//numberOfModels++;
		} else if (methodToExecute == REMOVE_MODEL_METHOD) {
			synchronizedModelsList.remove((Figure)arg);
			//numberOfModels--;
		} else if (methodToExecute == GET_MODEL_METHOD) {
			if (((Integer)arg) <= synchronizedModelsList.size()) { 
				return (synchronizedModelsList.get(((Integer)arg)));
			} 
			return null;
		} else if (methodToExecute == GET_NUM_OF_MODELS_METHOD) {
			return(synchronizedModelsList.size());
		}
		return (null);
	}
	
	/*
	 * returns the model that corresponds to the given id
	 */
	public Figure getModel (int modelId) {
		return ((Figure)accessGameModel(GET_MODEL_METHOD,modelId));
	}
	
	/*
	 * returns the number of models
	 */
	public int getNumOfModels() {
		return ((Integer)accessGameModel(GET_NUM_OF_MODELS_METHOD,null));
	}
}
